const koa = require("koa");
const app = new koa();
const fs = require("fs");
const path = require("path")
const router = require("koa-router")();
const koaBody = require("koa-body");
const static = require("koa-static")
router.post("/file", async ctx => {
    var file= ctx.request.files.logo.path
    var baseName = path.basename(file);
    console.log(baseName);
    //1创建一个可读流
    console.log(file);
   var reader= fs.createReadStream(file)
   //2 设置上传图片的文件名
   var upFilePath = `./static/${baseName}`;
   //3 创建一个可写流
   var writer = fs.createWriteStream(upFilePath)
   //4可读流通过管道写入可写流
   reader.pipe(writer)
    ctx.body = "上传成功"
})
app.use(static(path.join(__dirname,"static"))),
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 200 * 1024 * 1024,    //设置默认上传文件的大小
           // 保留文件扩展名
		keepExtensions: true
    }
}))
app.use(router.routes());

app.listen(4003);